/**
 * Created with JetBrains WebStorm.
 * User: Tomer
 * Date: 12/1/12
 * Time: 4:24 PM
 * To change this template use File | Settings | File Templates.
 */
function RegisterCtrl($scope,$location){
	$scope.username = "";
	$scope.password = "";

    if (globalAppParams.chatMode == 1){
        $location.path('/family');
    }
    else if (globalAppParams.chatMode == 2)
    {
        $location.path('/senior');
    }

    $scope.register = function(){


        if ($scope.name == "" || $scope.password == "" ||$scope.email == "" || $scope.seniorName == ""){
				$(".register-alerts .missing-error").hide();
				$(".register-alerts .missing-error").removeClass("hidden");
				$(".register-alerts .missing-error").fadeIn();
				
            return;
        }

        var result = $.ajax({
            url: '/saftalk/register',
            data: {name:$scope.name, password: $scope.password, email:$scope.email,phoneNumber:$scope.phoneNumber, seniorName: $scope.seniorName},
			dataType: "json",
            success: function(data, status, headers, config) {
            	if (data.success) {
                	$scope.$apply(function() {
        				fillGlobalAppParams(data,1);
                    	$location.path('/family');
                    	});            		
            	} else {
    				$(".register-alerts .email-error").hide();
    				$(".register-alerts .email-error").removeClass("hidden");
    				$(".register-alerts .email-error").fadeIn();            		
            	}
            }
        });
	}

}


function fillGlobalAppParams(data,chatMode){

		globalAppParams.name=data.name;
		globalAppParams.userId=data.userId;
		if (data.photoId!=null){
			globalAppParams.photoId=data.photoId
		} else {
			globalAppParams.photoId='';
		}
		
		globalAppParams.channel=data.channel;
		
		
		globalAppParams.chatMode=chatMode;
		globalAppParams.isAdmin=data.isAdmin;
		globalAppParams.fontSize=data.fontSize;
		globalAppParams.keyboard=data.keyboardEnabled;
					
}

function LoginCtrl($scope,$location, $routeParams,$http){

    if (globalAppParams.chatMode == 1){
        $location.path('/family');
    }
    else if (globalAppParams.chatMode == 2)
    {
        $location.path('/senior');
    }

	
    //$scope.modeOptions = [{ "value": 1, "text": "מצב משפחה" }, { "value": 2, "text": "מצב מבוגר" }];
	$scope.chatMode = 1;

	$scope.submitButton = new Object();
	initSubmitButton=function () {
	
	$scope.submitButton.label="התחברות";
	$scope.submitButton.disabled="";
	}
	disableSubmitButton=function () {
		$scope.submitButton.label="מתחבר...";
		$scope.submitButton.disabled="disabled";
	}
	initSubmitButton();
	

	

    $scope.login = function(){
	
        if ($scope.email == "" || $scope.password == ""){
				$(".login-alerts .alert-error").hide();
				$(".login-alerts .alert-error").removeClass("hidden");
				$(".login-alerts .alert-error").fadeIn();
            return;
        }
		disableSubmitButton();

	
			$http({method: 'GET', url: '/saftalk/getuser?email='+$scope.email+"&password="+$scope.password}).  success(function(data, status, headers, config) {
				
				
				
            	if (!data.success)
                {
				
					$(".login-alerts .alert-error").hide();
					$(".login-alerts .alert-error").removeClass("hidden");
					$(".login-alerts .alert-error").fadeIn();
					initSubmitButton();
                    return;
                }
				
					if (!($routeParams.qFamilyName!=undefined)) {
					$scope.chatMode = data.isSenior ? 2 : 1 ;
					}
				
				
					fillGlobalAppParams(data,$scope.chatMode);
					
				
					var animateTime=2000;
					
					if ($routeParams.qFamilyName!=undefined ) {
					 animateTime=6000;
					}
					$(".loginLoading .bar").animate({width:'100%'},animateTime,function(){
                        if ($scope.chatMode == 2)
                        {
                            $scope.$apply(function() {
                                $location.path('/senior');
                            });
                        }
                        else
                        {
                            $scope.$apply(function() {
                            $location.path('/family');
                            });
                        }
					})
					
					$(".loginForm").fadeOut(500,function(){
					$(".loginLoading").hide();
					$(".loginLoading").removeClass("hidden");
					$(".loginLoading").fadeIn();
					
				});
					
				
            
		  }).error(function(data, status, headers, config) {
			// called asynchronously if an error occurs
			// or server returns response with status
			// code outside of the <200, 400) range
		  });
		  }


			
		if ($routeParams.qFamilyName!=undefined ) {
	    $scope.email = $routeParams.qFamilyName;
		$scope.password = $routeParams.qPassword;
		$scope.kioskMode=true;
		$scope.chatMode=2;
	    $scope.login();
	};
	
}

function MainCtrl($scope,$location){
	if (globalAppParams.userId != 0)
    {
        if (globalAppParams.chatMode == 1){
            $location.path('/family');
        }
        else
        {
            $location.path('/senior');
        }
    }

    $scope.changePage = function(pageName){
	
		$location.path('/'+pageName);
	 }
}
